Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.6
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.6, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.05610 13.05117 13.04632 13.04154 13.03682 13.03217 13.02758 13.02306
## [9] 13.01859 13.01418 13.00983 13.00553 13.00129 12.99710 12.99296 12.98886
## [17] 12.98482 12.98082 12.97686 12.97294 12.96907 12.96523 12.96143 12.95766
## [25] 12.95393 12.95023 12.94656 12.94292 12.93931 12.93572 12.93216 12.92861
## [33] 12.92509 12.92159 12.91810 12.91463 12.91118 12.90776 12.90436 12.90100
## [41] 12.89767 12.89438 12.89112 12.88791 12.88473 12.88160 12.87852 12.87548
## [49] 12.87250 12.86956 12.86668 12.86385 12.86109 12.85838 12.85573 12.85315
## [57] 12.85064 12.84819 12.84581 12.84350 12.84127 12.83912 12.83704 12.83504
## [65] 12.83312 12.83129 12.82954 12.82789 12.82632 12.82484 12.82346 12.82214
## [73] 12.82084 12.81956 12.81832 12.81710 12.81591 12.81476 12.81364 12.81255
## [81] 12.81151 12.81051 12.80955 12.80864 12.80777 12.80695 12.80619 12.80548
## [89] 12.80482 12.80422 12.80368 12.80320 12.80278 12.80243 12.80214 12.80193
## [97] 12.80178 12.80171 12.80172 12.80180 12.80196 12.80220 12.80252 12.80293
## [105] 12.80343 12.80402 12.80470 12.80547 12.80633 12.80730 12.80836 12.80952
## [113] 12.81079 12.81224 12.81396 12.81592 12.81811 12.82051 12.82311 12.82589
## [121] 12.82884 12.83193 12.83516 12.83851 12.84196 12.84549 12.84910 12.85275
## [129] 12.85645 12.86017 12.86389 12.86761 12.87130 12.87495 12.87854 12.88206
## [137] 12.88549 12.88881 12.89201 12.89508 12.89917 12.90532 12.91330 12.92292
## [145] 12.93397 12.94622 12.95948 12.97352 12.98815 13.00315 13.01830 13.03340
## [153] 13.04825 13.06261 13.07630 13.08909 13.10078 13.11115 13.12000 13.12711
## [161] 13.13228 13.13741 13.14441 13.15306 13.16314 13.17444 13.18674 13.19983
## [169] 13.21348 13.22748 13.24161 13.25566 13.26941 13.28264 13.29514 13.30668
## [177] 13.31706 13.32605 13.33344 13.33901 13.34254 13.34382 13.34351 13.34244
## [185] 13.34062 13.33807 13.33482 13.33087 13.32626 13.32099 13.31508 13.30857
## [193] 13.30145 13.29376 13.28550 13.27671 13.26739 13.25757 13.24726 13.23649
## [201] 13.22527 13.21361 13.20155 13.18910 13.17627 13.16308 13.14956 13.13573
## [209] 13.12159 13.10717 13.09249 13.07757 13.06242 13.04706 13.03152 13.01581
## [217] 12.99995 12.98395 12.96784 12.95164 12.93536 12.91903 12.90265 12.88626
## [225] 12.86831 12.84745 12.82399 12.79823 12.77046 12.74098 12.71011 12.67812
## [233] 12.64534 12.61205 12.57857 12.54517 12.51218 12.47989 12.44859 12.41859
## [241] 12.39020 12.36370 12.33940 12.31760 12.29860 12.28108 12.26351 12.24595
## [249] 12.22842 12.21095 12.19359 12.17636 12.15930 12.14244 12.12582 12.10947
## [257] 12.09343 12.07773 12.06241 12.04749 12.03301 12.01929 12.00657 11.99480
## [265] 11.98393 11.97390 11.96467 11.95619 11.94839 11.94123 11.93465 11.92861
## [273] 11.92305 11.91792 11.91316 11.90873 11.90457 11.90063 11.89685 11.89319
## [281] 11.88960 11.88601 11.88238 11.87866 11.87479 11.87073 11.86641 11.86179
## [289] 11.85681 11.85143 11.84559 11.83923 11.83231 11.82478 11.81657 11.80765
## [297] 11.79841 11.78930 11.78029 11.77138 11.76254 11.75377 11.74505 11.73637
## [305] 11.72770 11.71904 11.71037 11.70167 11.69294 11.68415 11.67529 11.66634
## [313] 11.65730 11.64815 11.63886 11.62943 11.61985 11.61022 11.60067 11.59118
## [321] 11.58173 11.57233 11.56294 11.55357 11.54420 11.53481 11.52539 11.51594
## [329] 11.50643 11.49686 11.48722 11.47748 11.46764 11.45769 11.44761 11.43739
## [337] 11.42701 11.41653 11.40598 11.39537 11.38470 11.37397 11.36318 11.35233
## [345] 11.34142 11.33045 11.31943 11.30835 11.29722 11.28603 11.27479 11.26349
## [353] 11.25214 11.24074 11.22929 11.21779 11.20624 11.19464 11.18300 11.17130
## [361] 11.15956 11.14777 11.13594 11.12406 11.11214 11.10017 11.08817 11.07612
## [369] 11.06403 11.05190 11.03973 11.02752
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.6, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.62165 12.61714 12.61271 12.60838 12.60414 12.59999 12.59593 12.59195
## [9] 12.58806 12.58426 12.58055 12.57692 12.57337 12.56991 12.56653 12.56324
## [17] 12.56002 12.55689 12.55384 12.55087 12.54797 12.54516 12.54242 12.53976
## [25] 12.53718 12.53467 12.53223 12.52987 12.52758 12.52537 12.52323 12.52116
## [33] 12.51916 12.51723 12.51536 12.51357 12.51184 12.51018 12.50859 12.50706
## [41] 12.50560 12.50421 12.50290 12.50165 12.50049 12.49940 12.49838 12.49745
## [49] 12.49659 12.49582 12.49513 12.49453 12.49401 12.49358 12.49324 12.49299
## [57] 12.49283 12.49276 12.49279 12.49291 12.49314 12.49346 12.49388 12.49440
## [65] 12.49503 12.49575 12.49659 12.49753 12.49858 12.49975 12.50102 12.50244
## [73] 12.50404 12.50583 12.50778 12.50990 12.51219 12.51463 12.51723 12.51998
## [81] 12.52287 12.52590 12.52906 12.53236 12.53577 12.53931 12.54297 12.54673
## [89] 12.55061 12.55458 12.55865 12.56281 12.56705 12.57138 12.57579 12.58027
## [97] 12.58481 12.58942 12.59409 12.59881 12.60357 12.60839 12.61324 12.61812
## [105] 12.62303 12.62797 12.63293 12.63790 12.64289 12.64788 12.65286 12.65785
## [113] 12.66283 12.66803 12.67366 12.67969 12.68606 12.69274 12.69969 12.70685
## [121] 12.71420 12.72168 12.72925 12.73688 12.74451 12.75211 12.75963 12.76703
## [129] 12.77427 12.78131 12.78810 12.79460 12.80077 12.80656 12.81194 12.81781
## [137] 12.82505 12.83350 12.84302 12.85346 12.86469 12.87656 12.88893 12.90165
## [145] 12.91458 12.92758 12.94050 12.95320 12.96554 12.97738 12.98856 12.99896
## [153] 13.00842 13.01680 13.02395 13.02975 13.03642 13.04607 13.05833 13.07283
## [161] 13.08922 13.10712 13.12618 13.14602 13.16629 13.18661 13.20663 13.22598
## [169] 13.24429 13.26120 13.27635 13.28936 13.29989 13.30755 13.31199 13.31417
## [177] 13.31535 13.31553 13.31475 13.31304 13.31044 13.30696 13.30263 13.29750
## [185] 13.29157 13.28489 13.27749 13.26938 13.26061 13.25119 13.24116 13.23055
## [193] 13.21939 13.20770 13.19551 13.18286 13.16977 13.15627 13.14239 13.12815
## [201] 13.11360 13.09875 13.08364 13.06829 13.05273 13.03700 13.02111 13.00511
## [209] 12.98902 12.97286 12.95667 12.94048 12.92431 12.90819 12.89216 12.87624
## [217] 12.86046 12.84484 12.82943 12.81276 12.79353 12.77202 12.74851 12.72326
## [225] 12.69654 12.66862 12.63979 12.61031 12.58045 12.55048 12.52069 12.49133
## [233] 12.46268 12.43501 12.40860 12.38371 12.36063 12.33961 12.32094 12.30488
## [241] 12.29031 12.27593 12.26174 12.24775 12.23397 12.22039 12.20704 12.19392
## [249] 12.18102 12.16836 12.15595 12.14378 12.13188 12.12023 12.10885 12.09775
## [257] 12.08693 12.07640 12.06616 12.05729 12.05070 12.04616 12.04347 12.04239
## [265] 12.04270 12.04418 12.04660 12.04976 12.05341 12.05735 12.06135 12.06519
## [273] 12.06863 12.07148 12.07349 12.07445 12.07414 12.07233 12.06880 12.06333
## [281] 12.05735 12.05234 12.04816 12.04466 12.04170 12.03912 12.03679 12.03455
## [289] 12.03226 12.02979 12.02697 12.02367 12.01973 12.01502 12.00939 12.00269
## [297] 11.99542 11.98818 11.98094 11.97370 11.96643 11.95912 11.95175 11.94432
## [305] 11.93680 11.92918 11.92144 11.91357 11.90555 11.89737 11.88901 11.88046
## [313] 11.87170 11.86271 11.85348 11.84400 11.83425 11.82434 11.81440 11.80441
## [321] 11.79436 11.78424 11.77403 11.76373 11.75331 11.74276 11.73208 11.72124
## [329] 11.71024 11.69906 11.68769 11.67611 11.66431 11.65229 11.64002 11.62749
## [337] 11.61469 11.60166 11.58846 11.57510 11.56156 11.54786 11.53399 11.51995
## [345] 11.50575 11.49139 11.47686 11.46217 11.44732 11.43230 11.41713 11.40180
## [353] 11.38631 11.37067 11.35487 11.33891 11.32280 11.30654 11.29012 11.27356
## [361] 11.25684 11.23998 11.22296 11.20580 11.18849 11.17104 11.15344 11.13570
## [369] 11.11781 11.09978 11.08162 11.06331
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.6, n = 372)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.06120 12.05425 12.04741 12.04068 12.03406 12.02755 12.02115 12.01485
## [9] 12.00866 12.00257 11.99658 11.99069 11.98490 11.97920 11.97360 11.96809
## [17] 11.96268 11.95735 11.95211 11.94697 11.94190 11.93693 11.93203 11.92722
## [25] 11.92249 11.91783 11.91325 11.90875 11.90433 11.89997 11.89569 11.89147
## [33] 11.88733 11.88325 11.87924 11.87529 11.87140 11.86757 11.86381 11.86010
## [41] 11.85644 11.85285 11.84930 11.84583 11.84247 11.83920 11.83603 11.83297
## [49] 11.83001 11.82715 11.82439 11.82174 11.81919 11.81675 11.81441 11.81217
## [57] 11.81004 11.80802 11.80610 11.80428 11.80258 11.80098 11.79948 11.79810
## [65] 11.79682 11.79565 11.79459 11.79364 11.79279 11.79206 11.79144 11.79093
## [73] 11.79053 11.79024 11.79006 11.78999 11.79003 11.79019 11.79039 11.79055
## [81] 11.79069 11.79082 11.79093 11.79104 11.79116 11.79129 11.79144 11.79162
## [89] 11.79183 11.79209 11.79239 11.79275 11.79318 11.79368 11.79426 11.79492
## [97] 11.79568 11.79654 11.79751 11.79859 11.79980 11.80114 11.80262 11.80424
## [105] 11.80602 11.80796 11.81006 11.81234 11.81481 11.81746 11.82031 11.82337
## [113] 11.82663 11.83012 11.83383 11.83778 11.84197 11.84641 11.85111 11.85607
## [121] 11.86172 11.86843 11.87609 11.88462 11.89391 11.90388 11.91442 11.92545
## [129] 11.93686 11.94856 11.96047 11.97247 11.98448 11.99639 12.00813 12.01958
## [137] 12.03066 12.04128 12.05132 12.06071 12.07144 12.08537 12.10217 12.12152
## [145] 12.14309 12.16657 12.19162 12.21793 12.24516 12.27300 12.30112 12.32919
## [153] 12.35689 12.38390 12.40990 12.43455 12.45753 12.47853 12.49721 12.51325
## [161] 12.52632 12.53941 12.55546 12.57412 12.59505 12.61792 12.64235 12.66803
## [169] 12.69459 12.72170 12.74900 12.77615 12.80281 12.82863 12.85326 12.87636
## [177] 12.89758 12.91658 12.93301 12.94653 12.95679 12.96344 12.96729 12.96944
## [185] 12.96993 12.96883 12.96619 12.96209 12.95658 12.94973 12.94159 12.93222
## [193] 12.92169 12.91006 12.89738 12.88373 12.86916 12.85372 12.83750 12.82053
## [201] 12.80289 12.78464 12.76584 12.74654 12.72681 12.70672 12.68631 12.66566
## [209] 12.64483 12.62387 12.60284 12.58181 12.56085 12.54000 12.51933 12.49891
## [217] 12.47878 12.45903 12.43969 12.42085 12.40255 12.38486 12.36784 12.35155
## [225] 12.33445 12.31511 12.29376 12.27063 12.24598 12.22002 12.19299 12.16513
## [233] 12.13667 12.10785 12.07891 12.05007 12.02158 11.99366 11.96655 11.94050
## [241] 11.91572 11.89246 11.87096 11.85144 11.83415 11.81844 11.80346 11.78914
## [249] 11.77540 11.76214 11.74931 11.73681 11.72456 11.71248 11.70050 11.68853
## [257] 11.67649 11.66431 11.65189 11.63917 11.62605 11.61301 11.60054 11.58862
## [265] 11.57721 11.56631 11.55587 11.54588 11.53632 11.52715 11.51835 11.50991
## [273] 11.50179 11.49397 11.48643 11.47913 11.47207 11.46520 11.45852 11.45198
## [281] 11.44558 11.43928 11.43305 11.42689 11.42075 11.41462 11.40847 11.40228
## [289] 11.39602 11.38966 11.38320 11.37658 11.36981 11.36284 11.35566 11.34824
## [297] 11.34077 11.33347 11.32632 11.31932 11.31247 11.30574 11.29914 11.29266
## [305] 11.28629 11.28002 11.27385 11.26777 11.26176 11.25583 11.24997 11.24416
## [313] 11.23840 11.23269 11.22701 11.22135 11.21572 11.21016 11.20472 11.19940
## [321] 11.19419 11.18909 11.18409 11.17918 11.17436 11.16963 11.16497 11.16039
## [329] 11.15588 11.15143 11.14703 11.14269 11.13839 11.13414 11.12991 11.12572
## [337] 11.12155 11.11744 11.11341 11.10947 11.10562 11.10186 11.09819 11.09460
## [345] 11.09111 11.08771 11.08439 11.08117 11.07804 11.07500 11.07206 11.06920
## [353] 11.06644 11.06377 11.06120 11.05871 11.05633 11.05403 11.05183 11.04973
## [361] 11.04772 11.04581 11.04399 11.04227 11.04064 11.03912 11.03769 11.03635
## [369] 11.03512 11.03398 11.03295 11.03201
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")